package mini.hug;

import java.util.ArrayList;

import owg.util.Calc;
import owg.util.opengl.ColorF;
import world.Sound;
import hubol.Entity;
import hubol.Sprite;

public class Title extends Entity{
	private static final Sprite HEART = new Sprite("heart"), TEXT = new Sprite("instructions");
	
	private int xAt = 0;
	private ArrayList<ArrayList<Heart>> heart = new ArrayList<ArrayList<Heart>>();
	private ArrayList<Text> text = new ArrayList<Text>();
	
	private double animation;
	
	private int[] timer = new int[]{76, 41, 46, 33};
	private int textShow = 0;
	private final double tSc = .4, tX = 432, tY = 81, tInc = 81;
	
	public Title() {
		super(0, 0, 5);
		alarm[0] = 15;
		alarm[1] = 90;
		alarm[2] = 600;
		alarm[3] = alarm[2] + 86;
	}
	
	public void alarmEvent(int i){
		if (i == 0){
			ArrayList<Heart> h = getHeartFromXAt();
			if (h.size() != 0){
				Sound.playPitched("bg"+((xAt % 4) + 1), .006);
				heart.add(h);
				alarm[0] = (int) Math.max(5, 18 - Math.pow(xAt, 1.2));
			}
			xAt++;
		}
		else if (i == 1){
			if (textShow == 0)
				Sound.play("narrate instructions");
			Sound.playPitched("instruction");
			text.add(new Text(tX, tY + (tInc * textShow), textShow));
			textShow++;
			if (textShow < 5)
				alarm[1] = timer[textShow - 1];
		}
		else if (i == 2){
			Hug.me.play();
			heart.clear();
			text.clear();
		}
		else if (i == 3){
			Hug.me.generate();
		}
	}
	
	public void step(){
		for (ArrayList<Heart> l : heart){
			for (Heart h : l)
				h.step();
		}
		
		for (Text t : text)
			t.step();
		
		animation += .08;
		alarmStep();
	}
	
	public void render(){
		for (ArrayList<Heart> l : heart){
			for (Heart h : l)
				h.render(animation);
		}
		
		for (Text t : text)
			t.render();
	}
	
	private ArrayList<Heart> getHeartFromXAt(){
		ArrayList<Heart> out = new ArrayList<Heart>();
		int x = xAt, y = 0;
		while (x >= 0 && y <= 5){
			if (x <= 9)
				out.add(new Heart(getX(x), getY(y)));
			x--;
			y++;
		}
		return out;
	}
	
	private double getX(int x){
		return 43.2 + (x * 86.4);
	}
	
	private double getY(int y){
		return 40.5 + (y * 81);
	}
	
	private class Grow{
		protected double x, y, scale;
		
		public Grow(double x, double y){
			this.x = x;
			this.y = y;
			scale = 0;
		}
		
		public void step(){
			scale = Calc.approach(scale, 1, 4);
		}
	}
	
	private class Text extends Grow{
		private int i;
		private Shake s = Hug.me.shake();
		
		public Text(double x, double y, int image){
			super(x, y);
			i = image;
		}
		
		public void render(){
			TEXT.render(i, x + s.x, y + s.y, (float)(tSc * scale), (float)(tSc * scale), (float)(s.z / 20.0), ColorF.WHITE);
		}
	}
	
	private class Heart extends Grow{
		private double i, m;
		private Shake s = Hug.me.shake();

		public Heart(double x, double y) {
			super(x, y);
			i = Calc.random(4);
			m = 1 + Calc.rangedRandom(.2);
		}
		
		public void render(double animation){
			HEART.render((i + (animation * m)) % 4.0, x + s.x, y + s.y, (float)(.5 * scale), (float)(.5 * scale), (float)(s.z / 20.0), ColorF.WHITE);
		}
		
	}

}
